import { type Page, test as base } from '@playwright/test';
import testConfig from './config';

// 扩展测试夹具，添加登录方法
export const test = base.extend<{
  performLogin: (page: Page) => Promise<void>;
  performRegister: (page: Page) => Promise<void>;
}>({
  // 登录方法
  performLogin: async ({ }, use) => {
    const loginMethod = async (page: Page) => {
      // 等待登录模态框显示
      await page.waitForSelector(testConfig.selectors.loginModal, {
        timeout: testConfig.timeouts.modal,
      });
      
      // 填写登录表单
      await page.fill(testConfig.selectors.usernameInput, testConfig.auth.username);
      await page.fill(testConfig.selectors.passwordInput, testConfig.auth.password);
      
      // 点击登录按钮
      await page.click(testConfig.selectors.submitButton);
      
      // 等待登录模态框关闭
      await page.waitForSelector(testConfig.selectors.loginModal, {
        state: 'hidden',
        timeout: testConfig.timeouts.modal,
      });
      
      // 等待用户状态更新
      await page.waitForTimeout(1000);
    };
    
    await use(loginMethod);
  },
  
  // 注册方法
  performRegister: async ({ }, use) => {
    const registerMethod = async (page: Page) => {
      // 等待登录模态框显示
      await page.waitForSelector(testConfig.selectors.loginModal, {
        timeout: testConfig.timeouts.modal,
      });
      
      // 切换到注册标签
      await page.click(testConfig.selectors.registerTab, { hasText: '注册' });
      
      // 等待标签切换完成
      await page.waitForTimeout(500);
      
      // 等待注册表单显示
      await page.waitForSelector(testConfig.selectors.registerForm, {
        timeout: testConfig.timeouts.modal,
      });
      
      // 填写注册表单
      await page.fill(testConfig.selectors.registerUsername, testConfig.register.username);
      await page.fill(testConfig.selectors.registerEmail, testConfig.register.email);
      await page.fill(testConfig.selectors.registerFullname, testConfig.register.fullName);
      await page.fill(testConfig.selectors.registerFamily, testConfig.register.familyName);
      await page.fill(testConfig.selectors.registerPassword, testConfig.register.password);
      await page.fill(testConfig.selectors.registerConfirmPassword, testConfig.register.password);
      
      // 提交注册
      await page.click(testConfig.selectors.registerButton);
      
      // 等待注册成功（登录模态框应该关闭）
      await page.waitForSelector(testConfig.selectors.loginModal, {
        state: 'hidden',
        timeout: testConfig.timeouts.modal,
      });
      
      // 等待用户状态更新
      await page.waitForTimeout(1000);
    };
    
    await use(registerMethod);
  },
});

export const expect = base.expect;

// 公共辅助方法
export const helpers = {
  // 检查并处理登录弹窗
  async checkAndHandleLogin(page: Page, performLogin: (page: Page) => Promise<void>) {
    const loginModal = page.locator(testConfig.selectors.loginModal);
    if (await loginModal.isVisible()) {
      await performLogin(page);
    }
  },
  
  // 等待页面加载完成
  async waitForPageReady(page: Page) {
    await page.waitForSelector(testConfig.selectors.userStatus, {
      timeout: testConfig.timeouts.navigation,
    });
  },
  
  // 打开应用窗口并处理登录
  async openAppWindow(page: Page, appName: string, performLogin: (page: Page) => Promise<void>) {
    // 点击桌面图标
    await page.click(testConfig.selectors.desktopIcon, { hasText: appName });
    
    // 检查是否弹出登录框，如果是则进行登录
    await helpers.checkAndHandleLogin(page, performLogin);
    
    // 等待应用窗口打开
    await page.waitForSelector(testConfig.selectors.appWindow, {
      timeout: testConfig.timeouts.navigation,
    });
  },
  
  // 设置视口大小
  async setMobileViewport(page: Page) {
    await page.setViewportSize(testConfig.app.mobileViewport);
  },
  
  async setDesktopViewport(page: Page) {
    await page.setViewportSize(testConfig.app.viewport);
  },
  
  // 获取测试配置
  getConfig() {
    return testConfig;
  },
};

// 类型定义
export type TestConfig = typeof testConfig;